package com.es.jdbc;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.sql.*;

/**
 * ResultSet的使用
 */
public class TestResultSet {

    private static final String ORACLE_URL = "jdbc:oracle:thin:@127.0.0.1:1521:ORCL";
    private static final String USER = "HR";
    private static final String PASSWORD = "hr";

    public static void main(String[] args) {
        try {
            //1.
            Class.forName("oracle.jdbc.driver.OracleDriver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        try {
            //2.
            Connection connection = DriverManager.getConnection(ORACLE_URL, USER, PASSWORD);
            //3.
            Statement statement = connection.createStatement();
            //4.
            String sql = "select last_name,job_id,salary,hire_date from employees";
            ResultSet resultSet = statement.executeQuery(sql);
            //遍历结果集
            System.out.println("姓名\t岗位\t薪资");
            //是否有下一行，有就自动跳到下一行
            while(resultSet.next()){
                //操作的是当前行
                // 列的索引从1开始的。
                /*
                String lastName = resultSet.getString(1);
                String jobId = resultSet.getString(2);
                System.out.println(lastName+"\t"+jobId);
                  */
                String lastName = resultSet.getString("LAST_NAME");
                String jobId = resultSet.getString("job_id");
                BigDecimal salary = resultSet.getBigDecimal("salary");
                salary = salary.setScale(2, RoundingMode.HALF_UP);
                //日期
                //Date date = resultSet.getDate("hire_date");
                Timestamp tt = resultSet.getTimestamp("hire_date");
                //
                //resultSet.getTime()

                System.out.println(lastName+"\t"+jobId+"\t"+salary+"\t"+tt);

            }

            resultSet.close();
            statement.close();
            connection.close();

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
